﻿using Prism.Commands;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore.Helper;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Unity;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class UserGroupManageViewModel : ListRegionViewModel<UserGroup>
    {
        private readonly IOAuthService _authService;
        private DelegateCommand _createUserCommand;
        private DelegateCommand _editUserCommand;
        private DelegateCommand _deleteUserCommand;
        private DelegateCommand _setUserGroupRightCommand;
        public UserGroupManageViewModel(
           IRegionManager regionManager,
           IUnityContainer unityContainer,
           IDialogService dialogService,
           IOAuthService oAuthService
           ) :
           base(regionManager, unityContainer, dialogService)
        {
            _authService = oAuthService;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public ICommand CreateUserCommand
        {
            get
            {
                if (_createUserCommand != null) return _createUserCommand;
                _createUserCommand = new DelegateCommand(onCreateUserCommandAsync);
                return _createUserCommand;
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public ICommand EditUserCommand
        {
            get
            {
                if (_editUserCommand != null) return _editUserCommand;
                _editUserCommand = new DelegateCommand(onEditUserCommandAsync);
                return _editUserCommand;
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        public ICommand SetUserGroupRightCommand
        {
            get
            {
                if (_setUserGroupRightCommand != null) return _setUserGroupRightCommand;
                _setUserGroupRightCommand = new DelegateCommand(onSetUserGroupRightCommandAsync);
                return _setUserGroupRightCommand;
            }
        }


        /// <summary>
        /// 编辑
        /// </summary>
        public ICommand DeleteUserCommand
        {
            get
            {
                if (_deleteUserCommand != null) return _deleteUserCommand;
                _deleteUserCommand = new DelegateCommand(onDeleteUserCommandAsync);
                return _deleteUserCommand;
            }
        }
        /// <summary>
        /// 用户组的权限分配
        /// </summary>
        private void onSetUserGroupRightCommandAsync()
        {
            if (SelectObj == null)
            {
                Dialog.ShowAltert("请先选择需要编辑权限的用户组对象！！！");
                return;
            }

            var para = new DialogParameters
            {
                { "groupId", SelectObj.GroupId}
            };
            Dialog.ShowDialog("UserGroupSetRight", para,
                d =>
                {
                    if (d.Result == ButtonResult.OK)
                    {
                        OnInitPage(null);
                    }
                });
        }

        private void onCreateUserCommandAsync()
        {
            var para = new DialogParameters
            {
                { "groupId", "" }
            };
            Dialog.ShowDialog("UserGroupEdit", para,
                d =>
                {
                    if (d.Result == ButtonResult.OK)
                    {
                        OnInitPage(null);
                    }
                });
        }

        private void onEditUserCommandAsync()
        {
            if (SelectObj == null)
            {
                Dialog.ShowAltert("请先选择需要编辑的用户组对象！！！");
                return;
            }

            var para = new DialogParameters
            {
                { "groupId", SelectObj.GroupId}
            };
            Dialog.ShowDialog("UserGroupEdit", para,
                d =>
                {
                    if (d.Result == ButtonResult.OK)
                    {
                        OnInitPage(null);
                    }
                });
        }

        private void onDeleteUserCommandAsync()
        {
            if (SelectObj == null)
            {
                Dialog.ShowAltert("请先选择需要删除的用户组对象！！！");
                return;
            }

            Dialog.ShowQuestion("确认要删除当前选择的用户组？", async dr =>
            {
                if (dr.Result != ButtonResult.Yes)
                    return;

                try
                {
                    var r = await _authService.DeleteUseGrouprById(SelectObj.GroupId.ToString());
                    if (r.HasValue && r.Value)
                    {
                        Dialog.ShowAltert("删除用户组信息成功！！！");
                        OnInitPage(null);
                    }
                }
                catch (Exception x)
                {
                    var xMessage = $"删除用户组信息失败：{x.Message}！！！";
                    LoggerHelper.Current.Error(xMessage, x);
                    Dialog.ShowAltert(xMessage);
                }
            });


        }

        protected override async void OnInitPage(object obj)
        {
            try
            {
                var list = await _authService.GetAllUserGroupList();
                DataList.Clear();
                DataList.AddRange(list);
            }
            catch (Exception x)
            {
                var xMessage = $"模板数据加载失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
